package org.dragonnova.lamp;

import org.dragonnova.lamp.utils.LogUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.TreeMap;

/**
 * Created by hucn on 2016/10/26.
 * Description: 保存Lamp状态的类
 */
public class LampManager {

    public static final String TAG = LampManager.class.getSimpleName();

    private final TreeMap<Integer, LampGroup> LampsGroup = new TreeMap<>();

    public synchronized void refresh(LampGroup group) {
        if (group != null) {
            LampsGroup.put(group.getGroup(), group);
        }
    }

    public synchronized List<Lamp> getLamps() {
        List<Lamp> lamps = new ArrayList<>();
        Collection<LampGroup> groups = LampsGroup.values();
        for (LampGroup group : groups) {
            lamps.addAll(group.getLamps());
        }
        return lamps;
    }

    public synchronized List<LampGroup> getGroups() {
        return new ArrayList<>((LampsGroup.values()));
    }

    public synchronized TreeMap<Integer, LampGroup> getGroupMap() {
        return LampsGroup;
    }

    public synchronized void clear() {
        LampsGroup.clear();
    }

    public synchronized void refresh(int groupId, byte[] statusBytes) {
        LampGroup group = LampsGroup.get(groupId);
        if (group != null) {
            group.refresh(statusBytes);
        } else {
            LogUtil.e(TAG, "group is null:" + groupId);
        }
    }
}
